package common

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"store/libs/helper"
	"store/logic/common"
	"store/rest/base"
)

type OperateLog struct {
	base.Controller
	Logic *common.AdminOperateLogLogic
}

func NewOperateController() *OperateLog {
	control := new(OperateLog)
	control.Logic = &common.AdminOperateLogLogic{}
	return control
}

// @Summary 删除AdminOperateLog
// @Description 删除AdminOperateLog
// @Accept  application/json,application/xml
// @Produce json,xml
// @Tags    操作日志
// @Param  id   query    int     true        "AdminOperateLog id"
// @Success 200 {object} controller.Response
// @failure 400 {object} controller.Response
// @Router /admin/common/admin-operate-log [delete]
func (this *OperateLog) Delete(ctx *gin.Context) {
	id, err := this.GetInfoParams(ctx)
	if err != nil {
		this.Response(ctx, http.StatusBadRequest, err.Error(), nil)
		return
	}

	rId := helper.ToInt(id)
	if rId < 0 {
		this.Response(ctx, http.StatusBadRequest, "id最小为1", nil)
		return
	}

	this.Logic.Clone(ctx).Delete(uint64(rId))

	this.Response(ctx, http.StatusOK, "ok", nil)
}

// @Summary 获取AdminOperateLog详情
// @Description 获取AdminOperateLog详情
// @Tags    操作日志
// @Accept  json
// @Produce json
// @Param  id   query    int     true        "AdminOperateLogid"
// @Success 200 {object} controller.Response
// @failure 400 {object} controller.Response
// @Router /admin/common/admin-operate-log [get]
func (this *OperateLog) Get(ctx *gin.Context) {
	id, err := this.GetInfoParams(ctx)

	if err != nil {
		this.Response(ctx, http.StatusBadRequest, err.Error(), nil)
		return
	}

	info, exists := this.Logic.Clone(ctx).View(map[string]interface{}{
		"id": id,
	})

	if !exists {
		this.Response(ctx, http.StatusNotFound, "not found", nil)
		return
	}

	this.Response(ctx, http.StatusOK, "ok", info)
}

// @Summary 获取AdminOperateLog列表
// @Description 获取AdminOperateLog列表
// @Tags    操作日志
// @Accept  json
// @Produce json
// @Param  page       query    int     false        "页码"
// @Param  pageSize   query    int     false        "分页大小"
// @Success 200 {object} controller.Response
// @failure 400 {object} controller.Response
// @Router /admin/common/admin-operate-log/index [get]
func (this *OperateLog) GetIndex(ctx *gin.Context) {
	page, pageSize, where, orders := this.GetIndexParams(ctx)
	result := this.Logic.Clone(ctx).Index(page, pageSize, where, orders)

	this.Response(ctx, http.StatusOK, "ok", result)
}
